﻿using System;
using System.Reflection;
using System.Threading;
using System.Windows;
using MetroGA.Core;

namespace MetroGA.Wp8
{
	public static class MetroAnalytics
	{
		private static IMetroAnalytics _current;
		public static IMetroAnalytics Current
		{
			get
			{
				if (_current == null)
					throw new InvalidOperationException("Analytics not initialized");
				return _current;
			}
		}

		public static void Initialize(string trackerId, string applicationId = null, string applicationName = null, string applicationVersion = null)
		{
			if (applicationId == null || applicationName == null || applicationVersion == null)
			{
				var assembly = Application.Current.GetType().GetTypeInfo().Assembly;
				applicationId = applicationId ?? assembly.GetName().Name;
				applicationName = applicationName ?? assembly.GetCustomAttribute<AssemblyProductAttribute>().Product;
				applicationVersion = applicationVersion ?? assembly.GetName().Version.ToString();
			}

			LazyInitializer.EnsureInitialized(ref _current, () => new Analytics(trackerId, applicationId, applicationName, applicationVersion));
		}
	}
}

